package com.mxpio.erp.technology.controller;


import com.mxpio.erp.common.technology.entity.StandTecRout;
import com.mxpio.erp.common.technology.entity.StandTecRoutProc;
import com.mxpio.erp.common.technology.service.StandTecRoutProcService;
import com.mxpio.erp.common.technology.service.StandTecRoutService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author wupaa
 */
@Tag(name = "StandTecRoutController", description = "标准工艺路线接口")
@RestController
@RequestMapping("/erp/tech/standrout/")
public class StandTecRoutController {

    @Autowired
    private StandTecRoutService service;

    @Autowired
    private StandTecRoutProcService lineService;

    @GetMapping("list")
    @Operation(summary = "标准工艺路线列表", description = "获取标准工艺路线列表（无分页）", method = "GET")
    public Result<List<StandTecRout>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<StandTecRout> list = service.list(StandTecRout.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "标准工艺路线列表", description = "获取标准工艺路线列表（分页）", method = "GET")
    public Result<Page<StandTecRout>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<StandTecRout> listPage = service.listPage(StandTecRout.class, page, criteria);
        return Result.OK(listPage);
    }

    @GetMapping("list/{routCode}")
    @Operation(summary = "根据routCode获取标准工艺路线", description = "根据routCode获取标准工艺路线", method = "GET")
    public Result<StandTecRout> getById(@Parameter(description="通知单号") @PathVariable(name = "routCode", required = true) String routCode) {
        //带明细行数据
        StandTecRout data = service.getById(routCode);
        return Result.OK(data);
    }

    @DeleteMapping("remove/{routCodes}")
    @Operation(summary = "根据routCode删除标准工艺路线", description = "删除标准工艺路线", method = "DELETE")
    public Result<StandTecRout> remove(@Parameter(description="业务单号，多个单号用英文逗号分隔") @PathVariable(name = "routCodes", required = true) String routCodes) {
        String[] routCode = routCodes.split(",");
        for(String key : routCode){
            Criteria criteria = Criteria.create();
            criteria.addCriterion("routCode", Operator.EQ,key);
            lineService.removeBatch(StandTecRoutProc.class,criteria);
            service.delete(StandTecRout.class, key);
        }
        return Result.OK();
    }

    @RequestMapping("save")
    @Operation(summary = "保存标准工艺路线", description = "保存标准工艺路线")
    public Result<StandTecRout> save(@RequestBody StandTecRout rout) {
        service.save(rout,new SmartCrudPolicyAdapter(){
            @Override
            public boolean beforeInsert(CrudContext context) {
                Object o = context.getEntity();
                if(o instanceof StandTecRoutProc){
                    ((StandTecRoutProc) o).setRoutCode(rout.getRoutCode());
                }
                return true;
            }
        });
        return Result.OK(rout);
    }

}
